Squid代理和缓存服务的部署

1. Squid服务介绍

Squid是比较知名的代理软件,它不仅可以跑在Linux上,还可以跑在Windows以及Unix上,它的技术已经非常成熟。目前使用Squid的用户也是十分广泛的。Squid与Linux下其它的代理软件如Apache、Socks、TIS FWTK相比,下载安装简单,配置简单灵活,支持缓存和多种协议。

Squid之所以用的很多,是因为它的缓存功能。Squid缓存不仅可以节省宝贵的带宽资源,也可以大大降低服务器的IO。从经济角度考虑,它是很多网站架构中不可或缺的角色。

Squid不仅可以做正向代理,也可以做反向代理。

当作为正向代理时,Squid后面是客户端,客户端想上网需经过Squid。当一个用户(客户端)想要请求一个主页时,它向Squid发出一个申请,要Squid替它请求,然后Squid连接用户要请求的网站并请求该主页,接着把该主页传给用户同时保留一个备份,当别的用户请求同样的页面时,Squid把保存的备份立即传给用户,使用户觉得速度非常块。使用正向代理时,客户端需要做一些设置,才能实现,也就是平时我们在IE选项中设置的那个代理。

而反向代理是,Squid后面为某个站点的服务器,客户端请求该站点时,会先把请求发送到Squid上,然后Squid去处理用户的请求动作。

总之,简单来说,正向代理,Squid后面是客户端,客户端上网要通过Squid去上。反向代理,Squid后面是服务器,服务器返回给用户数据需要走Squid。

那什么时候需要配置正向代理,又什么时候配置反向代理呢?有这样一个观点就是:正向代理用在企业的办公环境中,员工上网需要通过Squid代理来上网,这样可以节省网络带宽资源。而反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,它用于网站架构中。


2. Squid正向代理

CentOS系统自带Squid包,安装命令是:yum install -y squid

当然你也可以源码包编译安装,Squid的官网是http://www/squid-cache.org,下载需要的版本。如果想要编译安装Squid,参考下面的编译参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
./configure --prefix=/usr/local/squid \
--disable-dependency-tracking \
--enable-dlmalloc \
--enable-gnuregex \
--disable-carp \
--enable-async-io=240 \
--with-pthreads \
--enable-storeio=ufs,aufs,diskd,null \
--disable-wccp \
--disable-wccpv2 \
--enable-kill-parent-hack \
--enable-cachemgr-hostname=localhost \
--enable-default-err-language=Simplify_Chinese \
--with-build-environment=POSIX_V6_ILP32_OFFBIG \
--with-maxfd=65535 \
--with-aio \
--disable-poll \
--enable-epoll \
--enable-linux-netfilter \
--enable-large-cache-files \
--disable-ident-lookups \
--enable-default-hostsfile=/etc/hosts \
--with-dl \
--with-large-files \
--enable-removal-policies=heap,lru \
--enable-delay-pools \
--enable-snmp \
--disable-internal-dns

注意:这些参数不见得符合你的需求,这里只是提供一个参考。其实,CentOS中自带的Squid足以满足需求,所以没有必要去编译安装。

安装完后,可以查看Squid版本(同时还可以看到Squid的编译参数):

1
2
3
4
5
]# squid -v
Squid Cache: Version 3.5.20
Service Name: squid
configure options: '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu'
......

下面需要配置一下Squid,来实现正向代理:

1
2
# rm -f /etc/squid/squid.conf
# vim /etc/squid/squid.conf

我们不使用默认的配置文件,删除它,重新写入如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
http_port 3128
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
acl localnet src 10.0.0.0/8 #RFC1918 possible internal network
acl localnet src 172.16.0.0/12 #RFC1918 possible internal network
acl localnet src 192.168.0.0/16 #RFC1918 possible internal network
acl SSL_ports port 443
acl Safe_ports port 80 8080 #http
acl Safe_ports port 21 #ftp
acl Safe_ports port 443 #https
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access allow localnet
http_access allow localhost
http_access allow all
cache_dir aufs /data/cache 1024 16 256
cache_mem 256M
hierarchy_stoplist cgi-bin ?
coredump_dir /var/spool/squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern \.(jpeg|png|gif|mp3|xml) 1440 50% 2880 ignore-reload
refresh_pattern . 0 20% 4320

在配置文件中有几处需要简单描述一下:

  • 第一行的http_port 3128这个指的是,Squid服务启动后将要监听的端口,也可以是80.
  • cache_dir这个用来指定本地磁盘上的缓存目录(需要手动创建,否则启动不了),后边的1024为大小,单位是M,具体根据你的磁盘大小决定。
  • cache_mem它用来规定缓存占用内存的大小,即把缓存的东西存到内存里,具体也需要根据你机器的内存定,如果你的机器只是跑Squid服务,那么留给系统512内存外,其他可以都分给Squid。

配置文件保存后,可以先检测一下是否有语法错误:

1
# squid -kcheck

如果提示信息为:

1
squid: ERROR: No running copy

这是说squid还未启动,没有关系,显示成这样说明配置文件没有问题。在启动前还得做一件事,就是初始化缓存目录:

1
2
3
# mkdir -p /data/cache
# chown -R squid:squid /data/cache/
# squid -z #<==初始化缓存目录,可以省略

在上述的初始化完成后,就可以启动squid了:

1
# /etc/init.d/squid start

查看squid是否启动:

1
# ps aux | grep squid

还有1个关于squid的命令需要掌握:

  • squid -k rec可以重新加载配置
  • service squid start启动服务
  • service squid restart重启服务

到这一步,就已经可以在真机上测测看squid的正向代理了。可以在IE浏览器里面进行设置。

或者直接使用curl命令测试即可:

1
# curl -xlocalhost:3128 http://www.baidu.com/

如果你看到了一大串,说明squid正向代理设置成功了。另外我们也可以观察squid对图片的缓存:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# curl -xlocalhost:3128 -I http://www.apelearn.com/bbs/static/image/common/logo.png
HTTP/1.0 200 OK
Server: nginx
Date: Thu, 22 Mar 2018 04:29:45 GMT
Content-Type: image/png
Content-Length: 4350
Last-Modified: Fri, 27 Mar 2015 06:03:50 GMT
ETag: "5514f2c6-10fe"
Expires: Sat, 21 Apr 2018 04:29:45 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: MISS from localhost.localdomain:3128
Via: 1.0 localhost.localdomain (squid/3.1.23)
Connection: keep-alive
# curl -xlocalhost:3128 -I http://www.apelearn.com/bbs/static/image/common/logo.png
HTTP/1.0 200 OK
Server: nginx
Date: Thu, 22 Mar 2018 04:29:45 GMT
Content-Type: image/png
Content-Length: 4350
Last-Modified: Fri, 27 Mar 2015 06:03:50 GMT
ETag: "5514f2c6-10fe"
Expires: Sat, 21 Apr 2018 04:29:45 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes
Age: 34
X-Cache: HIT from localhost.localdomain
X-Cache-Lookup: HIT from localhost.localdomain:3128 #<==注意这里的不同
Via: 1.0 localhost.localdomain (squid/3.1.23)
Connection: keep-alive

上面连续访问了两次同样的图片资源,可以发现前后两次的不同,其中X-Cache-Lookup: HIT from localhost.localdomain:3128显示,该请求已经HIT,它直接从本地的3128端口获取了数据。

有时我们会有这样的需求,就是想要限制某些域名不能通过代理访问,或者说只想代理某几个域名,这该如何做呢?在squid.conf中找到

1
acl CONNETC method CONNECT

在其下面添加四行:

1
2
3
4
acl http proto HTTP
acl good_domain dstdomain .apelearn.com .aminglinux.com
http_access allow http good_domain
http_access deny http !good_domain

其中,我的白名单域名为:.apelearn.com .aminglinux.com,这里的.表示万能匹配。前面可以是任意字符,你只需要填写你的白名单即可。

重启squid再来测测看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# /etc/init.d/squid restart
# curl -xlocalhost:3128 -I http://www.baidu.com/
HTTP/1.0 403 Forbidden
Server: squid/3.1.23
Mime-Version: 1.0
Date: Thu, 15 Mar 2018 12:08:47 GMT
Content-Type: text/html
Content-Length: 3286
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: NONE from localhost.localdomain:3128
Via: 1.0 localhost.localdomain (squid/3.1.23)
Connection: keep-alive

访问百度已经变成了403了。如果要设置黑名单呢?道理是一样的:

1
2
3
4
acl http proto HTTP
acl bad_domain dstdomain .sina.com .souhu.com
http_access allow http !bad_domain
http_access deny http bad_domain

重启squid后,测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# /etc/init.d/squid restart
# curl -xlocalhost:3128 http://www.sina.com/ -I
HTTP/1.0 403 Forbidden
Server: squid/3.1.23
Mime-Version: 1.0
Date: Thu, 15 Mar 2018 12:12:44 GMT
Content-Type: text/html
Content-Length: 3283
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: NONE from localhost.localdomain:3128
Via: 1.0 localhost.localdomain (squid/3.1.23)
Connection: keep-alive
# curl -xlocalhost:3128 http://www.baidu.com/ -I
HTTP/1.0 200 OK
Server: bfe/1.0.8.18
Date: Thu, 22 Mar 2018 05:21:12 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
ETag: "575e1f5c-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: MISS from localhost.localdomain:3128
Via: 1.0 localhost.localdomain (squid/3.1.23)
Connection: keep-alive

可以看到baidu.com可以访问,而sina.com不可以访问了。


3. Squid反向代理

反向代理配置过程其实和前面的正向代理没有什么太大的区别,唯一的区别是配置文件中一个地方需要修改一下。需要把:

1
http_port 3128

改为:

1
http_port 80 accel vhost vport

然后再增加你要代理的后端真实服务器信息:

1
2
3
4
cache_peer 180.97.33.108 parent 80 0 originserver name=a
cache_peer 101.226.103.106 parent 80 0 originserver name=b
cache_peer_domain a www.qq.com
cache_peer_domain b www.baidu.com

说明:

  • 因为咱们之前没有配置网站信息,所以就拿QQ和Baidu来做个例子。
  • 其中,chache_peer为配置后端的服务器IP及端口。
  • name 后边为要配置的域名,这里和后面的cache_peer_domain相对应。

实际的应用中,IP大多为内外IP,而域名也许会有多个,如果是squid要代理一台Web上的所有域名,那么就写成这样:

1
cache_peer 192.168.10.111 parent 80 0 originserver

后面连cache_peer_domain也省了。

反向代理主要用于缓存静态项,因为诸多静态项目尤其是图片、流媒体等比较耗费带宽,在中国,联通网访问电信的资源本来就慢,如果再去访问大流量的图片、流媒体那更会满了,所以如果在联通网配置一个Squid反向代理,让联通客户端直接访问这个联通Squid,而这些静态项已经被缓存在了Squid上,这样就大大加快了访问速度。也许你听说过CDN,其实它的设计原理就是这样的思路。

接下来,测试一下反向代理。因为修改了配置文件,所以需要重新加载squid配置文件或者重启squid服务:

1
2
3
4
# /etc/init.d/squid restart
# curl -xlocalhost:80 http://www.baidu.com/
# curl -xlocalhost:80 http://www.qq.com/
# curl -xlocalhost:80 http://www.sina.com

你会发现,baidu.com和qq.com都能正常访问,然而sina.com访问503了,这是因为我们并没有加sina.com的相关设置。


4. 关于Squid的操作

关于Squid还有一些知识点,需要掌握:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# squid -h
Usage: squid [-cdhvzCFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal]
-a port Specify HTTP port number (default: 3128).
-d level Write debugging to stderr also.
-f file Use given config-file instead of
/etc/squid/squid.conf
-h Print help message.
-k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse
Parse configuration file, then send signal to
running copy (except -k parse) and exit.
-s | -l facility
Enable logging to syslog.
-u port Specify ICP port number (default: 3130), disable with 0.
-v Print version.
-z Create swap directories
-C Do not catch fatal signals.
-D OBSOLETE. Scheduled for removal.
-F Don't serve any requests until store is rebuilt.
-N No daemon mode.
-R Do not set REUSEADDR on port.
-S Double-check swap during rebuild.
-X Force full debugging.
-Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.

上面把squid命令所用到的选项全部打印出来了。但是最常用的除了squid -k check外,还有一个那就是squid -k reconfigure。它们俩都可以简写为:

  • squid -kche
  • squid -krec
  • 且其中第二条命令表示重新加载配置文件,如果我们更改了配置文件后,不需要重启squid服务,直接使用这个命令重新加载配置即可。
0%